var renderChart = function(p){
	
	var that = {};
	
	//p = params
		//"title":"title",
		//"datatable":".sortable",
		//"item_title":0,
		//"item_data1":1,
		//"item_data2":2,
		//"item_data3":3,
		//"renderTo":'chart'
		//"height":int
		//"autoHeight":bool
		//"vDirection":int
		//"hDirection":int
		//"pie":bool
		//"top":int
		//"withLabel":bool
		
	if($(p.datatable).size() > 0){
		
		var data = new google.visualization.DataTable();
		var data_pie = new google.visualization.DataTable();
		var chart;
		
		var table = $(p.datatable);
		var rows = $('tbody tr',table).size();
		var title = $('thead tr th:eq(' + p.item_title + ')',table).text();
		var column1 = $('thead tr th:eq(' + p.item_data1 + ')',table).text();
		var column2 = $('thead tr th:eq(' + p.item_data2 + ')',table).text();
		var column3 = $('thead tr th:eq(' + p.item_data3 + ')',table).text();
		var height = (p.height) ? p.height : 500;
		var hDirection = (p.hDirection != -1) ? 1 : p.hDirection;
		var vDirection = (p.vDirection != -1) ? 1 : p.vDirection;
		if(p.autoHeight){
			height = rows*50;
		}
		
		$('#' + p.renderTo).height(height);
		
		data.addColumn('string', title);
		data.addColumn('number', column1);
		if(column2)data.addColumn('number', column2);
		if(column3)data.addColumn('number', column3);
		data.addRows(rows);
		
		$('tbody tr',table).each(function(i){
			data.setValue(i, 0, $('td:eq(' + p.item_title + ')',$(this)).text());
			data.setValue(i, 1, Number($('td:eq(' + p.item_data1 + ')',$(this)).text()));
			if(column2)data.setValue(i, 2, Number($('td:eq(' + p.item_data2 + ')',$(this)).text()));
			if(column3)data.setValue(i, 3, Number($('td:eq(' + p.item_data3 + ')',$(this)).text()));
		});

		if(p.pie == true){
			
			var pie_rows = rows;
			if(column2 && !column3)pie_rows *= 2;
			if(column3)pie_rows *= 3;
			
			data_pie.addColumn('string', "A");
			data_pie.addColumn('number', "B");
			if(column2){
				data_pie.addColumn('string', "C");
				data_pie.addColumn('number', "D");
			}
			if(column3){
				data_pie.addColumn('string', "E");
				data_pie.addColumn('number', "F");
			}
			data_pie.addRows(pie_rows);
			
			var r = 0;
			$('tbody tr',table).each(function(i){	
				
				var title = $('td:eq(' + p.item_title + ')',$(this)).text();
				var label1 = "",label2 = "",label3 = "";
				if(p.withLabel){
					label1 = column1;
					label2 = column2;
					label3 = column3;
				}
				
				data_pie.setValue(r, 0, title + label1);
				data_pie.setValue(r, 1, Number($('td:eq(' + p.item_data1 + ')',$(this)).text()));
				r += 1;				
				
				if(column2){
					data_pie.setValue(r, 0,title + label2);
					data_pie.setValue(r, 1, Number($('td:eq(' + p.item_data2 + ')',$(this)).text()));
					r += 1;
				}
				
				if(column3){
					data_pie.setValue(r, 0, title + label3);
					data_pie.setValue(r, 1, Number($('td:eq(' + p.item_data3 + ')',$(this)).text()));
					r += 1;
				}
			});
			
		}


		var left = 30;
		that.renderColumn = function(){
			chart = new google.visualization.ColumnChart(document.getElementById(p.renderTo));
			that.draw(data);
			that.setResizeEvent("column");
		};
		
		that.renderLine = function(){
			chart = new google.visualization.LineChart(document.getElementById(p.renderTo));
			that.draw(data);
			that.setResizeEvent("line");
		};
		
		that.renderBar = function(){
			left = 120;
			chart = new google.visualization.BarChart(document.getElementById(p.renderTo));
			that.draw(data);
			that.setResizeEvent("bar");
		};
		
		that.renderPie = function(){
			chart = new google.visualization.PieChart(document.getElementById(p.renderTo));
			that.draw(data_pie);
			that.setResizeEvent("pie");
		};
		
		that.draw = function(d){
			table.hide();
			$("#" + p.renderTo).show();
			chart.draw(d, {
				chartArea: {
					left:left,
					top:(p.top) ? p.top : 100,
					width:"100%"
				},
				legend: {
					position: 'right'
				},
				sort: true,
				is3D: true, 
				title: p.title, 
				vAxis : {direction: vDirection}, 
				hAxis : {direction: hDirection},
				fontSize:12
			});
		};
		
		that.setResizeEvent = function(t){			
			var timer = false;
			$(window).unbind('resize').resize(function() {
				if (timer !== false) {
					clearTimeout(timer);
				}
				timer = setTimeout(function() {
					if(t == "column"){that.renderColumn();}
					else if(t == "line"){that.renderLine();}
					else if(t == "bar"){that.renderBar();}
					else if(t == "pie"){that.renderPie();}
				}, 200);
			});
		};
	}
		
	return that;
		
}